// ----------------------------------
// RSDK Project: Sonic 1
// Script Description: HUD Object
// Script Author: Christian Whitehead/Simon Thomley
// Unpacked by Rubberduckycooly's script unpacker
// ----------------------------------

// ========================
// Aliases
// ========================

private alias object.value0 : object.drawFunction
private alias object.value1 : object.flashTimer
private alias object.value2 : object.timeOver

// Player Aliases
private alias object.type    : player.type
private alias object.xpos    : player.xpos
private alias object.ypos    : player.ypos
private alias object.value0  : player.rings

// Game Modes Aliases
private alias 2 : MODE_TIMEATTACK


// ========================
// Function Declarations
// ========================

reserve function HUD_Draw_Standard
reserve function HUD_Draw_Mobile


// ========================
// Function Definitions
// ========================

private function HUD_Draw_Standard
#platform: USE_ORIGINS
	// temp4 will tell whether to draw lives or coins
	temp4 = game.coinMode
	
	if game.playMode == BOOT_PLAYMODE_MISSION
		if game.missionEnd == false
			game.hudEnable = HUDENABLE_ON
		end if
		temp4 = true
	else
	
		if game.playMode == BOOT_PLAYMODE_BOSSRUSH
			game.hudEnable = HUDENABLE_ON
			temp4 = false
		else
		
			if game.coinMode != false
				game.hudEnable = HUDENABLE_ON
			end if
#endplatform

			DrawSpriteScreenXY(10, 17, 13)

#platform: USE_ORIGINS
			temp1 = stage.minutes
			temp2 = stage.seconds
			
			CheckGreater(temp1, 9)
			temp3 = checkResult
			CheckNotEqual(object.timeOver, false)
			checkResult |= temp3
			
			if checkResult != false
				temp1 = 9
				temp2 = 59
			end if
			
			if temp1 == 9
#endplatform
			
#platform: USE_STANDALONE
			if stage.minutes == 9
#endplatform
				if object.flashTimer > 7
					DrawSpriteScreenXY(11, 17, 29)
				end if
			end if

			if player[0].rings == 0
				if object.flashTimer > 7
					DrawSpriteScreenXY(12, 17, 45)
				end if
			end if

			DrawNumbers(0, 104, 13, player.score, 6, 8, false)
			
			if options.gameMode < MODE_TIMEATTACK
				// In standard mode, only seconds & minutes are shown, along with their corresponding ticks
				DrawSpriteScreenXY(14, 77, 29)
			else
				// If in time attack, draw extended time with milliseconds (and its extra ticks)
				DrawSpriteScreenXY(13, 77, 29)
				DrawNumbers(0, 104, 29, stage.milliSeconds, 2, 8, true)
			end if
			
			// Rest of the time display, kept the same between modes
#platform: USE_STANDALONE
			DrawNumbers(0, 90, 29, stage.seconds, 2, 8, true)
			DrawNumbers(0, 66, 29, stage.minutes, 1, 8, true)
#endplatform
#platform: USE_ORIGINS
			DrawNumbers(0, 90, 29, temp2, 2, 8, true)
			DrawNumbers(0, 66, 29, temp1, 1, 8, true)
#endplatform
			
			DrawNumbers(0, 90, 45, player[0].rings, 3, 8, false)

			if stage.debugMode == true
				// If the player is in debug mode, draw X and Y positions

				// First get the absolute X pos value, truncated down to essentially ixpos
				temp0 = player[0].xpos
				temp0 >>= 16
				Abs(temp0)

				// Get position to draw it
				temp1 = screen.xsize
				temp1 -= 24

				// Draw the XPos value
				DrawNumbers(0, temp1, 13, temp0, 5, 8, true)

				// Then get absolute Y pos value, also truncated down to iypos
				temp0 = player[0].ypos
				temp0 >>= 16
				Abs(temp0)

				// Draw the YPos value
				DrawNumbers(0, temp1, 29, temp0, 5, 8, true)

				temp1 -= 42

				// If XPos is in the negatives, draw the red version of the "X:" icon
				if player[0].xpos >= 0
#platform: USE_STANDALONE
					DrawSpriteScreenXY(18, temp1, 15)
#endplatform
#platform: USE_ORIGINS
					DrawSpriteScreenXY(21, temp1, 15)
#endplatform
				else
#platform: USE_STANDALONE
					DrawSpriteScreenXY(36, temp1, 15)
#endplatform
#platform: USE_ORIGINS
					DrawSpriteScreenXY(42, temp1, 15)
#endplatform
				end if

				// Same for YPos - if negative, draw red version of "Y:" icon
				if player[0].ypos >= 0
#platform: USE_STANDALONE
					DrawSpriteScreenXY(19, temp1, 31)
#endplatform
#platform: USE_ORIGINS
					DrawSpriteScreenXY(22, temp1, 31)
#endplatform
				else
#platform: USE_STANDALONE
					DrawSpriteScreenXY(37, temp1, 31)
#endplatform
#platform: USE_ORIGINS
					DrawSpriteScreenXY(43, temp1, 31)
#endplatform
				end if
			end if
#platform: USE_ORIGINS
		end if
	end if
	
	if temp4 == false
#endplatform
		// Draw life indicator
		temp0 = stage.playerListPos
		temp0 += 15
		DrawSpriteScreenXY(temp0, 16, 212)
#platform: USE_STANDALONE
		temp0 += 6
#endplatform
#platform: USE_ORIGINS
		temp0 += 9
#endplatform

		DrawSpriteScreenXY(temp0, 33, 213)
#platform: USE_STANDALONE
		DrawSpriteScreenXY(20, 38, 222)
		DrawNumbers(24, 56, 220, player.lives, 2, 8, false)
#endplatform
#platform: USE_ORIGINS
		DrawSpriteScreenXY(23, 38, 222)
		DrawNumbers(30, 56, 220, player.lives, 2, 8, false)
	else
		if game.playMode != BOOT_PLAYMODE_MISSION
			// Draw coins indicator
			DrawSpriteScreenXY(44, 16, 212)
			DrawSpriteScreenXY(45, 33, 213)
			DrawSpriteScreenXY(23, 36, 222)
			DrawNumbers(30, 59, 220, game.coinCount, 3, 8, false)
		end if
	end if
#endplatform
end function


private function HUD_Draw_Mobile
	DrawSpriteScreenXY(10, 17, 9)

	if stage.minutes == 9
		if object.flashTimer > 7
			DrawSpriteScreenXY(11, 17, 25)
		end if
	end if

	if player[0].rings == 0
		if object.flashTimer > 7
			DrawSpriteScreenXY(12, 17, 41)
		end if
	end if

	DrawNumbers(0, 104, 9, player.score, 6, 8, false)
	if options.gameMode < MODE_TIMEATTACK
		DrawSpriteScreenXY(14, 67, 25)
	else
		DrawSpriteScreenXY(13, 67, 25)
		DrawNumbers(0, 104, 25, stage.milliSeconds, 2, 8, true)
	end if
	DrawNumbers(0, 80, 25, stage.seconds, 2, 8, true)
	DrawNumbers(0, 56, 25, stage.minutes, 1, 8, true)
	DrawNumbers(0, 80, 41, player[0].rings, 3, 8, false)

	if player[0].type == TypeName[Debug Mode]
		temp0 = player[0].xpos
		temp0 >>= 16
		Abs(temp0)
		temp1 = screen.xsize
		temp1 -= 16
		DrawNumbers(0, temp1, 25, temp0, 5, 8, true)
		temp0 = player[0].ypos
		temp0 >>= 16
		Abs(temp0)
		DrawNumbers(0, temp1, 41, temp0, 5, 8, true)
		temp1 -= 42
		if player[0].xpos >= 0
#platform: USE_STANDALONE
			DrawSpriteScreenXY(18, temp1, 27)
#endplatform
#platform: USE_ORIGINS
			DrawSpriteScreenXY(21, temp1, 27)
#endplatform

		else
#platform: USE_STANDALONE
			DrawSpriteScreenXY(36, temp1, 27)
#endplatform
#platform: USE_ORIGINS
			DrawSpriteScreenXY(42, temp1, 27)
#endplatform
		end if

		// Same for YPos - if negative, draw red version of "Y:" icon
		if player[0].ypos >= 0
	
#platform: USE_STANDALONE
			DrawSpriteScreenXY(19, temp1, 43)
#endplatform
#platform: USE_ORIGINS
			DrawSpriteScreenXY(22, temp1, 43)
#endplatform
		else
					
#platform: USE_STANDALONE
			DrawSpriteScreenXY(37, temp1, 43)
#endplatform
#platform: USE_ORIGINS
			DrawSpriteScreenXY(43, temp1, 43)
#endplatform
		end if
#platform: USE_STANDALONE
			DrawSpriteScreenXY(34, screen.xcenter, 16)
			DrawSpriteScreenXY(35, screen.xcenter, 16)
#endplatform
#platform: USE_ORIGINS
			DrawSpriteScreenXY(40, screen.xcenter, 16)
			DrawSpriteScreenXY(41, screen.xcenter, 16)
#endplatform

	end if
	temp1 = screen.xsize
	temp1 -= 64
	temp0 = stage.playerListPos
	temp0 += 15
	DrawSpriteScreenXY(temp0, temp1, 8)
	temp1 += 17
#platform: USE_STANDALONE
	temp0 += 6
#endplatform
#platform: USE_ORIGINS
	temp0 += 9
#endplatform
	DrawSpriteScreenXY(temp0, temp1, 9)
	temp1 += 5
#platform: USE_STANDALONE
	DrawSpriteScreenXY(20, temp1, 18)
#endplatform
#platform: USE_ORIGINS
	DrawSpriteScreenXY(23, temp1, 18)
#endplatform

	temp1 += 18
#platform: USE_STANDALONE
	DrawNumbers(24, temp1, 16, player.lives, 2, 8, false)
#endplatform
#platform: USE_ORIGINS
	DrawNumbers(30, temp1, 16, player.lives, 2, 8, false)
#endplatform
end function


// ========================
// Events
// ========================

event ObjectDraw
	// Progress HUD flashing
	// 0 ring flashing takes priority over time over flashing
	if player[0].rings == 0
		object.flashTimer++
		if object.flashTimer == 16
			object.flashTimer = 0
		end if
	else
		if stage.minutes == 9
			object.flashTimer++
			if object.flashTimer == 16
				object.flashTimer = 0
			end if
		end if
	end if
	
#platform: USE_ORIGINS
	if game.timeOver != false
		object.timeOver = true
	end if
#endplatform

	CallFunction(object.drawFunction)
end event


event ObjectStartup
	LoadSpriteSheet("Global/Display.gif")

	// HUD frames (numbers are formatted as Standalone/Origins)
	SpriteFrame(0, 0, 8, 11, 1, 50) 	//  0/ 0 - Number 0
	SpriteFrame(0, 0, 8, 11, 10, 50) 	//  1/ 1 - Number 1
	SpriteFrame(0, 0, 8, 11, 19, 50) 	//  2/ 2 - Number 2
	SpriteFrame(0, 0, 8, 11, 28, 50) 	//  3/ 3 - Number 3
	SpriteFrame(0, 0, 8, 11, 1, 62) 	//  4/ 4 - Number 4
	SpriteFrame(0, 0, 8, 11, 10, 62) 	//  5/ 5 - Number 5
	SpriteFrame(0, 0, 8, 11, 19, 62) 	//  6/ 6 - Number 6
	SpriteFrame(0, 0, 8, 11, 28, 62) 	//  7/ 7 - Number 7
	SpriteFrame(0, 0, 8, 11, 1, 74) 	//  8/ 8  - Number 8
	SpriteFrame(0, 0, 8, 11, 10, 74) 	//  9/ 9  - Number 9
	SpriteFrame(0, 0, 49, 44, 313, 18) 	// 10/10 - Score, Times, Ring text
	SpriteFrame(0, 0, 41, 11, 363, 34) 	// 11/11 - Red Time text
	SpriteFrame(0, 0, 49, 12, 363, 50) 	// 12/12 - Red Rings text
	SpriteFrame(0, 0, 29, 4, 1, 45) 	// 13/13 - Timer tick marks
	SpriteFrame(0, 0, 3, 11, 37, 50) 	// 14/14 - : (Colon)
	SpriteFrame(0, 0, 16, 16, 43, 107) 	// 15/15 - Sonic life icon
	SpriteFrame(0, 0, 16, 16, 60, 107) 	// 16/16 - Tails life icon
	SpriteFrame(0, 0, 16, 16, 77, 107) 	// 17/17 - Knuckles life icon
#platform: USE_ORIGINS
	SpriteFrame(0, 0, 16, 16, 0, 0)		// XX/18 - Padding for Amy icon (her character ID is 5)
	SpriteFrame(0, 0, 16, 16, 0, 0)		// XX/19 - Padding for Amy icon (her character ID is 5)
	SpriteFrame(0, 0, 16, 16, 239, 174)	// XX/20 - Amy life icon
#endplatform
	SpriteFrame(0, 0, 8, 8, 1, 98) 		// 18/21 - Yellow "X" debug icon
	SpriteFrame(0, 0, 8, 8, 10, 98) 	// 19/22 - Yellow "Y" debug icon
	SpriteFrame(0, 0, 6, 6, 19, 98) 	// 20/23 - White small x debug icon
	SpriteFrame(0, 0, 31, 7, 144, 43) 	// 21/24 - Sonic name tag

	// Determine between Tails or Miles based on region setting
	if options.region == 0
		SpriteFrame(0, -1, 33, 8, 176, 43) // 22/25 - Tails name tag
	else
		SpriteFrame(0, -1, 34, 8, 177, 52) // 22/25 - Miles name tag
	end if

	SpriteFrame(0, 0, 32, 7, 144, 51) 	// 23/26 - Knuckles name tag
#platform: USE_ORIGINS
	SpriteFrame(0, 0, 31, 7, 0, 0)		// 27 - Padding for Amy name tag (her character ID is 5)
	SpriteFrame(0, 0, 31, 7, 0, 0)		// 28 - Padding for Amy name tag (her character ID is 5)
	SpriteFrame(0, 0, 31, 7, 182, 183)	// XX/29 - Amy name tag
#endplatform
	SpriteFrame(0, 0, 8, 8, 37, 64) 	// 24/30 - Small number 0
	SpriteFrame(0, 0, 8, 8, 46, 64) 	// 25/31 - Small number 1
	SpriteFrame(0, 0, 8, 8, 55, 64) 	// 26/32 - Small number 2
	SpriteFrame(0, 0, 8, 8, 64, 64) 	// 27/33 - Small number 3
	SpriteFrame(0, 0, 8, 8, 73, 64) 	// 28/34 - Small number 4
	SpriteFrame(0, 0, 8, 8, 82, 64) 	// 29/35 - Small number 5
	SpriteFrame(0, 0, 8, 8, 91, 64) 	// 30/36 - Small number 6
	SpriteFrame(0, 0, 8, 8, 100, 64) 	// 31/37 - Small number 7
	SpriteFrame(0, 0, 8, 8, 109, 64) 	// 32/38 - Small number 8
	SpriteFrame(0, 0, 8, 8, 118, 64) 	// 33/39 - Small number 9
	SpriteFrame(-16, 0, 8, 8, 127, 64) 	// 34/40 - Small debug minus sign
	SpriteFrame(8, 0, 8, 8, 136, 64) 	// 35/41 - Small debug plus sign
	SpriteFrame(0, 0, 8, 8, 145, 64) 	// 36/42 - Red "X" debug icon
	SpriteFrame(0, 0, 8, 8, 154, 64) 	// 37/43 - Red "Y" debug icon

#platform: USE_ORIGINS
	SpriteFrame(0, 0, 16, 16, 199, 141) // XX/44 - Coins life icon
	SpriteFrame(0, 0, 49, 7, 182, 158)  // XX/45 - Coins name tag
#endplatform

	foreach (TypeName[HUD], arrayPos0, ALL_ENTITIES)
		if engine.deviceType == STANDARD
			object[arrayPos0].drawFunction = HUD_Draw_Standard
		else
			object[arrayPos0].drawFunction = HUD_Draw_Mobile
		end if
	next
end event


// ========================
// Editor Events
// ========================

event RSDKDraw
	DrawSprite(0)
end event


event RSDKLoad
	LoadSpriteSheet("Global/Display.gif")
	SpriteFrame(0, 0, 39, 43, 1, 1)
	
	// Strangely enough, this does get set in Stage Setup
	// It's just an old leftover behaviour from CD, though, and has no real effect on anything
	SetVariableAlias(ALIAS_VAR_PROPVAL, "unused")
end event
